<template>
  <div>
      <section>
        <!--工具条-->
        <el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
          <el-form :inline="true" :model="filters">
            <el-form-item>
              <el-input v-model="filters.keyword" placeholder="规格名称"></el-input>
            </el-form-item>
            <el-form-item>
              <el-button type="primary" v-on:click="querySpec">查询</el-button>
            </el-form-item>
            <el-form-item>
              <el-button type="primary" @click="handleEdit()">新增</el-button>
            </el-form-item>
          </el-form>
        </el-col>

        <!--列表-->
        <el-table :data="spec" highlight-current-row v-loading="listLoading" @selection-change="selsChange" style="width: 100%;">
          <el-table-column type="selection" width="55">
          </el-table-column>
          <el-table-column type="index" width="60">
          </el-table-column>

          <el-table-column prop="id" label="规格id" width="160" sortable>
          </el-table-column>

          <el-table-column prop="specName" label="规格名称" width="160" sortable>
          </el-table-column>

          <el-table-column label="操作">
            <template scope="scope">
              <el-button size="small" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
              <el-button type="danger" size="small" @click="handleDel(scope.$index, scope.row)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>

        <!--工具条-->
        <el-col :span="24" class="toolbar">
          <el-button type="danger" :disabled="this.sels.length===0" @click="batchRemove">批量删除</el-button>
          <el-pagination layout="prev, pager, next" :current-page="pageNum" @current-change="handleCurrentChange" :page-size="pageSize" :total="total" style="float:right;">
          </el-pagination>
        </el-col>

<!--        &lt;!&ndash;编辑界面&ndash;&gt;-->
        <el-dialog title="编辑" :visible.sync="editSpecVisible" :close-on-click-modal="false">
          <el-form :model="editSpec" label-width="80px" :rules="editSpecRules" ref="editSpec">


            <el-form-item label="规格名称" prop="specName">
              <el-input v-model="editSpec.specName" auto-complete="off"></el-input>
            </el-form-item>

            <el-button type="primary" plain @click="addEditSpec">新增规格选项</el-button>

            <el-table :data="editSpec.specOption" style="width: 100%;">
              <el-table-column prop="optionName" label="规格选项" width="160" sortable>
                <template scope="scope">
                  <el-input size="small" v-model="scope.row.optionName"  type="text"></el-input>
                </template>
              </el-table-column>

              <el-table-column prop="orders" label="排序" width="160" sortable>
                <template scope="scope">
                  <el-input size="small" v-model="scope.row.orders"  type="text"></el-input>
                </template>
              </el-table-column>

              <el-table-column label="操作">
                <template scope="scope">
                  <el-button type="danger" size="small" @click="specOptionDel(scope.$index)">删除</el-button>
                </template>
              </el-table-column>
            </el-table>



          </el-form>
          <div slot="footer" class="dialog-footer">
            <el-button @click.native="editSpecVisible = false">取消</el-button>
            <el-button type="primary" @click.native="editSubmit" :loading="editLoading">提交</el-button>
          </div>
        </el-dialog>

      </section>
  </div>
</template>
<script>
import axios from "axios";

export default {
  name: "SpecView",
  data(){
    return{
      filters: {
        keyword: ''
      },
      spec:[],
      listLoading:false,
      sels:[],
      total: 0,
      pageNum: 1,
      pageSize:3,
      editLoading:false,
      editSpecVisible:false,
      editSpec:{
        specName: null,
        specOption:[{},{},{}],
      },
      editSpecRules: {
        specName: [
          { required: true, message: '请输入规格名称', trigger: 'blur' }
        ]
      },
    }
  },
  methods:{
    // 查询
    querySpec(){
      // 准备参数
      let params = {
        pageSize: this.pageSize, //页面大小
        pageNum: this.pageNum,   //页码
        specName:this.filters.keyword,
      }
      this.$http.post("/system/api/spec/queryPage",params)
          .then(resp=>{
            this.spec = resp.data.data.list;
            this.total = resp.data.data.total;
          }).catch(resp=>{
            this.$message({
              message: "请登录",
              type: 'error'
            });
      })
    },
    selsChange: function (sels) {
      this.sels = sels;
    },

    addEditSpec:function () {
      this.editSpec.specOption.push({})
    },

    handleCurrentChange(val) {
      this.pageNum = val;
      this.querySpec();
    },

    specOptionDel:function (index) {
      this.editSpec.specOption.splice(index,1)
    },

    //编辑
    editSubmit: function () {
      this.$refs.editSpec.validate((valid) => {
        if (valid) {
          this.$confirm('确认提交吗？', '提示', {}).then(() => {
            this.editLoading = true;
            //NProgress.start();
            let para = Object.assign({}, this.editSpec);
            this.$http.put("/system/api/spec/save",para)
                .then(resp=>{
                  if (resp.data.code==200){
                    this.$message({
                      message: resp.data.msg,
                      type: 'success'
                    });
                    this.editLoading=false;
                    this.editSpecVisible=false;
                    this.querySpec();
                  }else {
                    this.$message({
                      message: resp.data.msg,
                      type: 'error'
                    });
                    this.editLoading=false;
                    this.editSpecVisible=false;
                  }
                }).catch(resp=>{
                    this.editLoading=false;
                    this.$message({
                      message: "网络异常，稍后重试",
                      type: 'error'
              });
            })
          });
        }
      });
    },

    //删除
    handleDel: function (index, row) {
      this.$confirm('确认删除该记录吗?', '温馨提示', {
        type: 'warning'
      }).then(() => {
        this.listLoading = true;
        let url = "/system/api/spec/delete/"+row.id;
        this.$http.delete(url)
            .then(resp=>{
                  //去除忙等框
                  this.listLoading = false;
                  if (resp.data.code==200){
                    this.$message({
                      message: resp.data.msg,
                      type: 'success'
                    });
                    this.querySpec();
                  }else {
                    this.$message({
                      message: resp.data.msg,
                      type: 'error'
                    });
                  }
                }
            )
            .catch(result=>{
              //去除忙等框
              this.listLoading = false;
              //提示
              this.$message({
                message: '系统错误！',
                type: 'error'
              });
            })

      }).catch(() => {

      });
    },

    //批量删除
    batchRemove: function () {
      var ids = this.sels.map(item => item.id);
      this.$confirm('确认删除选中记录吗？', '提示', {
        type: 'warning'
      }).then(() => {
        this.listLoading = true;
        //NProgress.start();
        let para = {ids: ids };
        this.$http.patch("/system/api/spec/batchRemove",ids)
            .then(resp=>{
                  //去除忙等框
                  this.listLoading = false;
                  if (resp.data.code==200){
                    this.$message({
                      message: resp.data.msg,
                      type: 'success'
                    });
                    this.querySpec();
                  }else {
                    this.$message({
                      message: resp.data.msg,
                      type: 'error'
                    });
                    this.listLoading = false;
                  }
                }
            )
      }).catch(() => {
        this.listLoading = false;
        //提示
        this.$message({
          message: '系统错误！',
          type: 'error'
        });
      })
    },

    //显示编辑界面
    handleEdit: function (index, row) {
      if(row!=null){
        this.$http.post("/system/api/spec/queryOptionBySpecId/"+row.id)
            .then(res=>{
              if (res.data.code==200){
                this.editSpec=Object.assign({},row);
                this.editSpec.specOption= Object.assign([], res.data.data);
              }
            }).catch(res=>{
                this.$message({
                  message: "网络异常，稍后重试",
                  type: 'error'
                });
              })
      }else {
        this.editSpec= Object.assign([],row);
        this.editSpec.specOption=[{},{},{}]
      }
      this.editSpecVisible = true;
    },
  },
  mounted() {
      this.querySpec();
  }
}
</script>

<style scoped>

</style>